package rasterprimate;

import java.util.ArrayList;

import repast.simphony.engine.environment.RunEnvironment;
import repast.simphony.parameter.Parameters;
import repast.simphony.space.gis.Geography;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;

public class FoodSourceMemTopo {
	Coordinate coord;
	double maxFood;
	double regrowRate;
	double foodRemembered;
	double foodCapacity;
	int gridX,gridY;
	int agentID;
	ArrayList<FoodSourceMemTopo> nearBySites;
	Resource resRemembered;
	
	
	public FoodSourceMemTopo(Resource r){
		
		//get/set parameters
		Parameters p = RunEnvironment.getInstance().getParameters();
		regrowRate = (Double)p.getValue("regrowRate");
		coord = r.getCoord();
		maxFood= r.getresLevel();
		foodRemembered = r.getresLevel();
		foodCapacity = r.getmaxResLevel();
		nearBySites = new ArrayList<FoodSourceMemTopo>();
		gridX = r.getGridx();
		gridY = r.getGridy();
		resRemembered = r;
	}
	
	public void step(){
		
		//food remembered will grow back at the same rate as the "regrowRate"
			if (foodRemembered + regrowRate< maxFood){
				foodRemembered = foodRemembered + regrowRate;
		}else{
			foodRemembered = maxFood;
		}
			
	}
	
	//Run at the beginning of the simulation to build the structure of site associations
	public void setNearBySites(Geography geog, ArrayList<FoodSourceMemTopo> mem){
		Envelope nearSites = new Envelope();
		nearSites.init(this.getCoord());
		nearSites.expandBy(Constants.nearSites);
		Iterable<Resource> resources = geog.getObjectsWithin(nearSites,Resource.class);
		
		for (Resource r: resources){
			for (FoodSourceMemTopo fm: mem){
				if (fm.getResource().getCoord()==r.getCoord()){
					addNearBySite(fm);
				} 
			}
		}
		nearSites.setToNull();
	}
	
	public void addNearBySite(FoodSourceMemTopo m){
		nearBySites.add(m);
	}
	public ArrayList<FoodSourceMemTopo> getNearBySites(){
		return nearBySites;
	}	
	public void setFoodRemembered(double f){
		foodRemembered=f;
	}
	public double getFoodRemembered(){
		return foodRemembered;
	}
	public Coordinate getCoord(){
		return coord;
	}
	public void setMaxFood(double m){
		maxFood = m;
	}
	public void setAgentId(int id){
		agentID = id;
	}
	public int getAgentId (){
		return agentID;
	}
	public int getGridX(){
		return gridX;
	}
	public int getGridY(){
		return gridY;
	}
	public Resource getResource(){
		return resRemembered;
	}
	public double getFoodCapacity(){
		return foodCapacity;
	}
	public void setFoodCapacity(double c){
		foodCapacity = c;
	}
}
